﻿using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using AdventureWorksLT.Views.Customers.ViewModels;
using AdventureWorksLT.Web.Services;

namespace AdventureWorksLT.Views.Customers.ChildWindows
{
    public partial class SalesOrderHeaderAddCW : ChildWindow
    {
        
        readonly int loadedCustomerID = AdventureWorksLT.Views.Customers.ViewModels.CustomersViewModel.DashLoad_PK;
        //private int loadedPatient_Id = 1;
        private EntityQuery<AdventureWorksLT.Web.Models.Address> _shipToAddressQuery;
        private EntityQuery<AdventureWorksLT.Web.Models.Address> _billToAddressQuery;
        private AdventureWorksLTDomainContext _context;
        
        public SalesOrderHeaderAddCW()
        {
            InitializeComponent();
        LoadComboBoxData();
            
        }


        private void OKButton_Click(object sender, RoutedEventArgs e)
            {
                CustomerIDTextBox.Text = loadedCustomerID.ToString(); //make sure is not the primary key of the current form, it needs to be a FOREIGN key.
                ShipToAddressIDTextBox.Text = ShipToAddressIDComboBox.SelectedIndex.ToString();
                BillToAddressIDTextBox.Text = BillToAddressIDComboBox.SelectedIndex.ToString();
            if (!SalesOrderHeadersViewModel.SalesOrderHeadersContext.IsLoading)
                {
                    this.SalesOrderHeaderDataForm.CommitEdit();
                    this.DialogResult = true;
                }
            }
            
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        
        private void LoadComboBoxData()
        {

            //ShipToAddressIDComboBox
             _context = new AdventureWorksLTDomainContext();
            _shipToAddressQuery = _context.GetAddressesQuery();
            _shipToAddressQuery = _shipToAddressQuery.OrderBy(e => e.AddressID);
            _context.Load(_shipToAddressQuery, LoadBehavior.RefreshCurrent, ShipToAddressIDComboBox_Loaded, null);
            ShipToAddressIDComboBox.ItemsSource = _context.Addresses;
            //BillToAddressIDComboBox
             _context = new AdventureWorksLTDomainContext();
             _billToAddressQuery = _context.GetAddressesQuery();
            _billToAddressQuery = _billToAddressQuery.OrderBy(e => e.AddressID);
            _context.Load(_billToAddressQuery, LoadBehavior.RefreshCurrent, BillToAddressIDComboBox_Loaded, null);
            BillToAddressIDComboBox.ItemsSource = _context.Addresses;
        }


            private void ShipToAddressIDComboBox_Loaded(LoadOperation<AdventureWorksLT.Web.Models.Address> lo)
            {
                this.ShipToAddressIDComboBox.SelectedIndex = 1;
            }
            private void BillToAddressIDComboBox_Loaded(LoadOperation<AdventureWorksLT.Web.Models.Address> lo)
            {
                this.BillToAddressIDComboBox.SelectedIndex = 1;
            }

            private void SalesOrderHeaderDataForm_EditEnded(object sender, DataFormEditEndedEventArgs e)
            {
                this.Close();
            }

    }
}

